/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.controller.bean;

import com.entites.Employee;
import com.entites.EventLibrary;
import com.entites.Notifications;
import com.model.bean.NotificationTypeFacadeLocal;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Chung
 */
@ManagedBean
@SessionScoped
public class NotificationController {

    public final int EVENTNEW = 1;
    public final int BIRTHDAYNOTICES = 2;
    public final int EVENTREMINDERS = 3;
    public final int BIRTHDAYWISHES = 4;
    public final int EVENTINVITATIONS = 5;
    public final int EVENTUPDATE = 6;
    public final int EVENCOMMENT = 7;
    private final String USERNAME = "username";
    private final String LOGINSESSION = "login";
    private int count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0,count6;

    @EJB
    private NotificationTypeFacadeLocal notificationTypeFacadeLocal;

    private String message;

    FacesContext fCtx = FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession) fCtx.getExternalContext().getSession(true);

    public HttpSession getSession() {
        return session;
    }

    public void setSession(HttpSession session) {
        this.session = session;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * Creates a new instance of NotificationController
     */
    public NotificationController() {
    }

    public List<Notifications> getNotificationEventNew() {
        List<Notifications> listNotification;
        listNotification = notificationTypeFacadeLocal.getNotifications(EVENTNEW, getUsername());
        count1 = listNotification.size();
        return listNotification;
    }

    public List<Notifications> getNotificationBirthdayNotices() {
        List<Notifications> listNotification;
        listNotification = notificationTypeFacadeLocal.getNotifications(BIRTHDAYNOTICES, getUsername());
        count2 = listNotification.size();
        return listNotification;
    }

    public List<Notifications> getNotificationEventReminders() {
        List<Notifications> listNotification;
        listNotification = notificationTypeFacadeLocal.getNotifications(EVENTREMINDERS, getUsername());
        count3 = listNotification.size();
        return listNotification;
    }

    public List<Notifications> getNotificationBirthdayWishes() {
        List<Notifications> listNotification;
        listNotification = notificationTypeFacadeLocal.getNotifications(BIRTHDAYWISHES, getUsername());
        count4 = listNotification.size();
        return listNotification;
    }

    public List<Notifications> getNotificationEventInvitations() {
        List<Notifications> listNotification;
        listNotification = notificationTypeFacadeLocal.getNotifications(EVENTINVITATIONS, getUsername());
        count5 = listNotification.size();
        return listNotification;
    }
     public List<Notifications> getNotificationEventUpdate() {
        List<Notifications> listNotification;
        listNotification = notificationTypeFacadeLocal.getNotifications(EVENTUPDATE, getUsername());
        count6 = listNotification.size();
        return listNotification;
    }

    public int getcountNotification() {
        return count1 + count2 + count3 + count4 + count5+count6;
    }

    public int changeStatus(int IdNotification) {
        return notificationTypeFacadeLocal.changeStatus(IdNotification);
    }

    public String getUsername() {
        return (String) session.getAttribute(USERNAME);
    }

    public int addBirthdaywishes(int notificationId) {
        Employee em = getEmpoyeeInSession();
        Notifications notifications = new Notifications();
        notifications.setAvatarImg(em.getAvatar());
        notifications.setFullname(em.getFullname());
        notifications.setUsernameFE(em);
        notifications.setMessage(message);
        changeStatus(notificationId);
        return notificationTypeFacadeLocal.addBirthdaywishes(notifications);
    }

    public void checkNotification1() {
        notificationTypeFacadeLocal.checkNotification1();
    }

    private Employee getEmpoyeeInSession() {
        Employee em = new Employee();
        Login login = (Login) session.getAttribute(LOGINSESSION);
        em.setAvatar(login.getAvatar());
        em.setFullname(login.getFullname());
        em.setUsername(login.getUsername());
        return em;
    }

    public void sendInvitedAttendEvent(String usernameFe, int eventId) {
        try {
           String usernameMe=getUsername();
            message = usernameMe + "has invited you to attend one event";
            notificationTypeFacadeLocal.sendInvitedAttendEvent(usernameMe, usernameFe, message, eventId);
        } catch (Exception e) {
            System.out.println("Error controller" + e.getMessage());
        }
    }
}
